﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadMap.Data
{
    public struct Point { int x; int y; }
    [JsonObject(MemberSerialization.OptIn)]
    public class Station
    {
        public float x { get; set; }
        public float y { get; set; }
        public string MapID { get; set; }
        [JsonProperty("r")]
        public float r { get; set; }
        public string FindID { get; set; }
        [JsonProperty("title")]
        public string Name { get; set; }
        public string TextTransform { get; set; }
        public string ParentTransform { get; set; }
        public float TextX { get; set; }
        public float TextY { get; set; }
        [JsonProperty("text_x")]
        public float RealTextX
        {
            get
            {
                float offsetX = 0, offsetY = 0;
                float x, y;
                transformToReal(TextTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                transformToReal(ParentTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                return offsetX + TextX;
            }
        }
        [JsonProperty("text_y")]
        public float RealTextY
        {
            get
            {
                float offsetX = 0, offsetY = 0;
                float x, y;
                transformToReal(TextTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                transformToReal(ParentTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                return offsetY + TextY;
            }
        }
        [JsonProperty("x")]
        public float RealX
        {
            get
            {
                float offsetX = 0, offsetY = 0;
                float x, y;
                transformToReal(ParentTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                return offsetX + this.x;
            }
        }
        [JsonProperty("y")]
        public float RealY
        {
            get
            {
                float offsetX = 0, offsetY = 0;
                float x, y;
                transformToReal(ParentTransform, out x, out y);
                offsetX += x;
                offsetY += y;
                return offsetY + this.y;
            }
        }
        [JsonProperty("offset")]
        public Point Offset { get; set; }
        [JsonProperty("line")]
        public List<int> Line { get; set; }
        [JsonProperty("font_size")]
        public int FontSize { get; set; }
        [JsonProperty("text_bold")]
        public bool TextBold { get; set; }
        [JsonProperty("id")]
        public string StationID { get; set; }
        [JsonProperty("pinyin")]
        public string Pinyin { get; set; }
        [JsonProperty("en_name")]
        public string EnName { get; set; }
        [JsonProperty("number_id")]
        public int NumberID { get; set; }
        public static void transformToReal(string text, out float x, out float y)
        {
            if (string.IsNullOrEmpty(text))
            {
                x = 0; y = 0;
                return;
            }
            var start = text.IndexOf('(');
            var values = text.Substring(start + 1, text.Length - start - 2).Split(',');

            x = Convert.ToSingle(values[0]);
            y = Convert.ToSingle(values[1]);
        }
    }
}
